package com.tianyi.util;

import com.google.common.base.Splitter;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommUtils {

    private static final Logger logger = LoggerFactory.getLogger(CommUtils.class);

    private static final String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
    public static final Pattern PHONE_PATTERN = Pattern.compile(regex);

    public static final Splitter COMMA_SPLITTER = Splitter.on(",").trimResults().omitEmptyStrings();
    /**
     * IpUtils工具类方法
     * 获取真实的ip地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }

    /**
     * 中文转拼音
     * @param source
     * @return
     */
    public static String getPinYin(String source) {
        if (StringUtils.isBlank(source)) {
            return "";
        }
        char[] t1 = source.toCharArray();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuilder result = new StringBuilder();
        for (char aT1 : t1) {
            if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]")) {
                try {
                    result.append(PinyinHelper.toHanyuPinyinStringArray(aT1, format)[0]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    logger.error("转换汉字拼音错误!", e);
                }
            } else {
                // 非汉字不进行转换，直接添加
                result.append(aT1);
            }
        }
        return result.toString();
    }

    /**
     * 判断是否手机号
     *
     * @param phone
     * @return
     */
    public static boolean isPhoneNum(String phone){

        if (StringUtils.isBlank(phone) || phone.length()!=11){

            return false;
        }

        Matcher matcher = PHONE_PATTERN.matcher(phone);
        boolean matches = matcher.matches();
        return matches;
    }


    /*public static void main(String[] args) {
        String pinYin = getPinYin("崔旭辉");
        String pinYin2 = getPinYin("cuixuhui");
        System.out.println(pinYin);
        System.out.println(pinYin2);

        boolean phoneNum = isPhoneNum("1234567");
        System.out.println(phoneNum);
        boolean phone2 = isPhoneNum("15917105558");
        System.out.println(phone2);
    }*/

}
